﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Collections;
using System.Data.SqlClient;
using Utils;

/// <summary>
    /// Summary description for SQlHelper
    /// </summary>
    public class SQlHelper
    {
        public static readonly string ConnectionStringProfile = Common.Conn;

        private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());

        public static object ExecuteScalar(string Sql)
        {
            SqlConnection Connection = new SqlConnection(ConnectionStringProfile);
            SqlCommand Command = new SqlCommand(Sql, Connection);
            object result = null;
            try
            {
                Connection.Open();
                result = Command.ExecuteScalar();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Connection.Close();
            }
            return result;
        }

        public static DataTable GetAll(string sql)
        {
            DataTable retVal = null;
            SqlConnection dbConn = new SqlConnection(ConnectionStringProfile);
            SqlCommand dbCmd = new SqlCommand(sql, dbConn);
            dbCmd.CommandType = CommandType.Text;
            try
            {
                retVal = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public static int ExecuteNonQuery(string sql)
        {
            SqlConnection dbConn = new SqlConnection(ConnectionStringProfile);
            SqlCommand dbCmd = new SqlCommand(sql, dbConn);
            dbCmd.CommandType = CommandType.Text;
            int n = 0;
            try
            {
                dbConn.Open();
                n = dbCmd.ExecuteNonQuery();
                return n;
            }
            catch
            {
                return n;
            }
            finally
            {
                dbConn.Close();
            }
        }
      
        protected SqlConnection GetConnectionString()
        {
            SqlConnection conn = new SqlConnection(ConnectionStringProfile);
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
            conn.Open();
            return conn;
        }
    }

